//
//  NovelViewCategory.m
//  JianChaRiBao
//
//  Created by Changxy on 13-5-29.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "NovelViewCategory.h"
#import "NovelCell.h"
#import "NovelInfoViewController.h"
#import "AppDelegate.h"
@implementation NovelViewCategory
@synthesize category = _category;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
        categoryGridView = [[AQGridView alloc]initWithFrame:self.bounds];
//        if ([[UIDevice currentDevice].systemVersion intValue] >= 7.0) {
//            categoryGridView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
//        }
        categoryGridView.delegate = self;
        categoryGridView.dataSource = self;
        categoryGridView.leftContentInset = 20;
        categoryGridView.rightContentInset = 20;
        [self addSubview:categoryGridView];
        categoryGridView.backgroundViewExtendsDown = YES;
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bookshelf_cell_bg"]];
        categoryGridView.backgroundView = bgView;

        [categoryGridView reloadData];
    }
    return self;
}
-(void)setCategory:(NSArray *)category{
    
    if (category && category.count != 0) {
        [_category release];
        _category = [category retain];
    }
    [categoryGridView setContentOffset:CGPointZero];
    [categoryGridView reloadData];
}

-(NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView{
    return self.category.count;

}
-(AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index{
    static NSString *CellIdentify = @"ListenCell";
    NovelCell * cell = (NovelCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[NovelCell alloc]initWithFrame:CGRectMake(0, 0, 70, 100) reuseIdentifier:CellIdentify];
    }
    cell.novelInfo = [self.category objectAtIndex:index];
    [cell updateDisplayWithImageStr:@"cover_image"];
    
    return cell;
    
}

-(CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView{
    return CGSizeMake(80, 150);
}
- (CGRect)gridView:(AQGridView *)gridView adjustCellFrame:(CGRect)cellFrame withinGridCellFrame:(CGRect)gridCellFrame
{
    cellFrame.origin.y -= 5;
    return cellFrame;
}

-(void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index{
    NovelInfoViewController * novelInfoViewController = [[[NovelInfoViewController alloc] initWithNibName:@"NovelInfoViewController" bundle:nil] autorelease];
    novelInfoViewController.basicInfo = [self.category objectAtIndex:index];
    [[UIApplication rootNavController] pushViewController:novelInfoViewController animated:YES];
    [gridView deselectItemAtIndex:index animated:NO];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
